<?php

namespace app\index\controller;

use app\index\model\Member;
use app\index\model\Plan as PlanModel;
use app\index\model\Reflect as ReflectModel;
use app\index\service\Plan as PlanService;
use app\index\service\Reflect as ReflectService;
use app\index\validate\PlanVlidate as PlanVlidate;
use think\captcha\Captcha;
use think\Controller;
use think\Exception;


class Plan extends Basic
{

    protected $user_id;

    public function __construct()
    {
        parent::__construct();
        $this->user_id = session('member_id');
    }

    public function add_plan()
    {
        $title = "添加";
        $data = input('post.');
        $field = [ 'plan_str', 'plan_time', 'cycle' ];
        $data = array_field($data, $field);
        if ($this->request->isAjax())
        {
            $plan_validate = new PlanVlidate();
            $plan_validate->goCheck();
            $plan_model = new PlanModel();
            $data['plan_time'] = to_timestamp($data['plan_time']);
//            halt_plus($data);
            if ($plan_model->save($data))
            {
                return json([ 'code' => 1, 'msg' => '添加成功' ]);
            }
        }
        $this->assign(compact([
            'title',
        ]));
        return $this->fetch();
    }

    public function edit_plan($id)
    {
        $title = "修改";
        $data = input('post.');
        $field = [ 'plan_str', 'plan_time', 'cycle', 'id' ];
        $data = array_field($data, $field);
        $one = PlanModel::get($id);
        if ($this->request->isAjax())
        {
            $plan_validate = new PlanVlidate();
            $plan_validate->goCheck();
            $data['plan_time'] = to_timestamp($data['plan_time']);
//            halt_plus($data);
            //只能修改自己的计划
            if ($this->user_id == $one->member_id && $one->save($data))
            {
                return json([ 'code' => 1, 'msg' => '修改成功' ]);
            }
            else
            {
                return json([ 'code' => 0, 'msg' => '修改失败' ]);
            }
        }
        $this->assign(compact([
            'title',
            'one',
        ]));
        return $this->fetch();
    }

    public function delete_plan($id)
    {
        $one = PlanModel::find($id);
        //只能删除自己的计划
        if ($this->user_id == $one->member_id && $one->delete())
        {
            return json([ 'code' => 1, 'msg' => '删除成功' ]);
        }
        else
        {
            return json([ 'code' => 0, 'msg' => '删除失败' ]);
        }
        $this->assign(compact([
            'title',
            'one',
        ]));
        return $this->fetch();
    }

    public function index()
    {
        return $this->redirect('plan_list');
    }

    public function change_state()
    {
        if (input('id'))
        {
            return PlanService::change_state($this->user_id,input('id'));
        }
    }

    public function plan_list()
    {       
        $title = "列表";
        $state = input('state', 0);
        $plan_list = PlanService::getPlanList($state, $this->user_id);
        $reflect_num = ReflectService::getTodayNum($this->user_id);
        $this->assign(compact([
            'title',
            'plan_list',
            'reflect_num',
        ]));
        $list_html = $this->fetch('plan_list');
        $this->assign('list_html', $list_html);
        if ($this->request->isAjax())
        {
            return $list_html;
        }
        else
        {
            return $this->fetch('plan_list_base');
        }
    }

    /**
     * 统计
     * @return type
     */
    public function plan_stats()
    {
        $start_date = "";
        $end_date = "";
        $date = date("Y-m-d H:i:s");
        if (input('date', 5) == 6)
        {
            $timestamp = strtotime('-1 month');
            $date = date("Y-m-d H:i:s", $timestamp);
        }
        list($start_date, $end_date) = get_the_month($date);
        $year = to_date($start_date, "Y");
        $month = to_date($start_date, "n");
        $where = [
            [ 'member_id', '=', $this->user_id, ],
        ];
        $cout_daily = PlanService::count_daily($where, $start_date, $end_date);
        $cout_year = PlanService::count_year($where, $year);
        $this->assign(compact([
            'cout_daily',
            'cout_year',
            'year',
            'month',
        ]));
        $list_html = $this->fetch('plan_stats');
        $this->assign('list_html', $list_html);
        if ($this->request->isAjax())
        {
            return $list_html;
        }
        else
        {
            return $this->fetch('plan_list_base');
        }
    }

    /**
     * 挑战详情
     * @return type
     */
    public function plan_detail()
    {
        $start_date = "";
        $end_date = "";
        $date = date("Y-m-d H:i:s");
        $id = input('id', '');
        if (input('date', 5) == 6)
        {
            $timestamp = strtotime('-1 month');
            $date = date("Y-m-d H:i:s", $timestamp);
        }
        list($start_date, $end_date) = get_the_month($date);
        $year = to_date($start_date, "Y");
        $month = to_date($start_date, "n");
        $where = [
            [ 'member_id', '=', $this->user_id, ],
        ];
        $one = PlanModel::get($id);
        if (!empty($id) && !empty($one))
        {
            $where[] = [ 'plan_str', '=', $one->plan_str, ];
        }
        else
        {
            throw new Exception('非法id');
        }
        $plan_title = $one->plan_str;
        $title = $plan_title . "-详情";
        $cout_daily = PlanService::count_daily($where, $start_date, $end_date);
        $cout_year = PlanService::count_year($where, $year);
        $this->assign(compact([
            'plan_title',
            'cout_daily',
            'cout_year',
            'year',
            'month',
            'title',
        ]));
        $list_html = $this->fetch('plan_detail');
        return $list_html;
    }

}
